package com.apes.fico.masterdata.gl.respository;

import com.apes.fico.masterdata.gl.model.AccountingOperatingChart;
import com.apes.framework.jpa.repository.CustomRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Map;

public interface AccountingOperatingChartRepository extends CustomRepository<AccountingOperatingChart, String> {
//    @Query("select T from AccountingOperatingChart T where 1=1 and (code=:value)")
//    AccountingOperatingChart findByCode(@Param("value") String Code);
//
//    List<AccountingOperatingChart> findAllByAccSystem_IdAndCompany_Id(String id, String company);

    @Query("select new map( T.id as id,T.code as code,T.name as name,T.auxFlag as auxFlag ) from AccountingOperatingChart T" +
            " where T.finalFlag = 1 and  T.company.id = :company and (T.code like concat(:value,'%') or T.name like concat('%',concat(:value,'%'))) ")
    List<Map<String, Object>> findByCompanyAndCodeOrName(@Param("company") String company, @Param("value") String value);

    @Query("select new map( T.id as id,T.code as code ,T.name as name, T.finalFlag as finalFlag) from AccountingOperatingChart T where T.company.id=:company and T.accGroup.id=:accGroup order by T.id")
    List<Map<String, String>> findByCompany_IdAndAccGroup_Id(@Param("company") String company, @Param("accGroup") String accGroup);

    @Query("select new map( T.id as id,T.code as code ,T.name as name,T.finalFlag as finalFlag) from AccountingOperatingChart T where T.company.id=:company ORDER BY T.id")
    List<Map<String, String>> findByCompany_Id(@Param("company") String company);

    @Query("select new map( T.id as id,T.code as code ,T.name as name,T.finalFlag as finalFlag,T.auxFlag as auxFlag ) from AccountingOperatingChart T where T.company.id=:company and(T.finalFlag = 1)")
    List<Map<String, Object>> findByCompany(@Param("company") String company);

    @Query(value = "select DISTINCT T.code as id ,T.name as name from MAS_ACC_OPERATING_CHART T where T.CLASSES = 3 AND (CODE LIKE %?1%  or NAME LIKE %?1%) ORDER BY T.CODE",
            countQuery = "select COUNT(DISTINCT T.CODE) from MAS_ACC_OPERATING_CHART T where T.CLASSES = 3 AND (CODE LIKE %?1%  or NAME LIKE %?1%)",
            nativeQuery = true)
    Page<Map> findAllDistinctCode(String value , Pageable pageable);

    @Query("SELECT T FROM AccountingOperatingChart T WHERE T.code = :code AND T.company.id = :companyId")
    AccountingOperatingChart findAllByAccChart(@Param("code") String code,@Param("companyId") String companyId);
}
